package com.itheima.demo04;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/14 19:46
 */
public class MyRunnable implements Runnable {
    private String path;
    private CountDownLatch countDownLatch;
    private ConcurrentHashMap<String, String> map;
    public MyRunnable(String path,
                      CountDownLatch countDownLatch,
                      ConcurrentHashMap<String, String> map) {
        this.map = map;
        this.countDownLatch = countDownLatch;
        this.path = path;
    }


    @Override
    public void run() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(path));
            File file = new File(path);
            bos = new BufferedOutputStream(new FileOutputStream("E:\\copy\\" + file.getName()));
            byte[] arr = new byte[1024 * 8];
            int len;
            while ((len = bis.read(arr)) != -1) {
                bos.write(arr, 0, len);
                bos.flush();
            }
            //告诉主线程自己执行完毕了;
            countDownLatch.countDown();
            map.put(file.getName(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

